package tech.lq0.dreamaticvoyage.network.packet;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;
import tech.lq0.dreamaticvoyage.entity.projectile.OminousWaveEntity;

import java.util.function.Supplier;

public class OminousWavePacket {

    private final int type;
    private final float damage;

    public OminousWavePacket(int type, float damage) {
        this.type = type;
        this.damage = damage;
    }

    public static OminousWavePacket decode(FriendlyByteBuf buf) {
        return new OminousWavePacket(buf.readInt(), buf.readFloat());
    }

    public static void encode(OminousWavePacket packet, FriendlyByteBuf buf) {
        buf.writeInt(packet.type);
        buf.writeFloat(packet.damage);
    }

    public static void handle(OminousWavePacket packet, Supplier<NetworkEvent.Context> ctx) {
        ctx.get().enqueueWork(() -> {
            var player = ctx.get().getSender();
            if (player != null) {
                if (player.getAttackStrengthScale(0f) < 1.0) return;

                OminousWaveEntity wave = new OminousWaveEntity(player.level(), player, packet.type).damage(packet.damage);
                wave.shoot(player.getLookAngle().x, player.getLookAngle().y, player.getLookAngle().z, 1.8f, 0f);
                player.level().addFreshEntity(wave);
            }
        });
        ctx.get().setPacketHandled(true);
    }
}
